package cn.lagou

import scala.collection.mutable
import scala.collection.immutable
import scala.io.StdIn
import scala.util.Random
import util.control.Breaks._


object Question2 {

  // 出拳
  private val punchs: Array[String] = Array("石头", "剪刀", "布")

  // 角色
  private val roles: Array[String] = Array("刘备", "关羽", "张飞")

  private val resultText: Map[Int, String] = immutable.Map(1 -> "恭喜你，赢啦", 0 -> "和局，继续努力", -1 -> "很遗憾你输，加油")

  def main(args: Array[String]): Unit = {
    val game = new Question2
    game.welcome()
    val role = game.selectRole()
    game.start(role)
  }
}

class Details(var score: Int, var win: Int, var draw: Int, var lose: Int)

class Question2 {
  // 分数统计
  private val statistics = mutable.Map("" -> new Details(0, 0, 0, 0))

  def welcome(): Unit = {
    println("*" * 30)
    println("*" * 11 + "猜拳, 开始" + "*" * 11)
    println("*" * 30)
  }

  def selectRole(): String = {
    println("请选择对战角色:(1.刘备, 2.关羽, 3.张飞)")
    val role = StdIn.readInt()
    if (role < 1 || role > 3) {
      return "输入有误，请重新输入"
    }
    Question2.roles(role - 1)
  }

  def start(role: String): Unit = {
    println(s"你选择了与${role}对战")
    println("要开始么? y/n")
    val c = StdIn.readChar()

    if (c == 'y') {
      while (true) {
        breakable {
          println("请出拳！1.剪刀 2.石头 3.布")
          var punch = StdIn.readInt()
          punch -= 1
          if (punch < 0 || punch > 2) {
            println("输入有误，请重新输入")
            break // continue
          } else {
            println("你出拳:" + Question2.punchs(punch))
            println(s"${role}出拳")

            val robotGetPunch = robotPunch()
            println(s"${role}出拳:$robotGetPunch")
            val result: Int = confront(Question2.punchs(punch), robotGetPunch)

            println("结果:" + Question2.resultText(result))

            record(result, "游客", role)

            println("是否开始下一轮? y/n")

            val c1 = StdIn.readChar()
            if (c1 == 'n') {
              println("退出游戏!")
              showDetail("游客", role)
              return // break
            }
          }
        }
      }
    }
  }

  /**
   * 机器人出拳
   */
  def robotPunch(): String = {
    // 随机取出数组元素
    Random.shuffle(Question2.punchs.toList).head
  }

  /**
   * 对抗
   * 1 人类获胜
   * 0 平局
   * -1 人类失败
   */
  def confront(human: String, robot: String): Int = {
    if (human == robot) 0
    else if ((human == "石头" && robot == "剪刀")
      || (human == "剪刀" && robot == "布")
      || (human == "布" && robot == "石头")) 1
    else -1
  }

  def showDetail(human: String, robot: String): Unit = {
    println("--" * 16)
    println(s"$human VS $robot")
    println("对战次数" + (statistics(s"$human").win + statistics(s"$human").draw + statistics(s"$human").lose) + "次")
    println()
    println("姓名\t得分\t胜局\t和局\t负局")
    println(s"${human}\t" + statistics(s"$human").score + "\t" + statistics(s"$human").win + "\t" + statistics(s"$human").draw + "\t" + statistics(s"$human").lose)
    println(s"${robot}\t" + statistics(s"$robot").score + "\t" + statistics(s"$robot").win + "\t" + statistics(s"$robot").draw + "\t" + statistics(s"$robot").lose)
    println("--" * 16)
  }

  def record(result: Int, role: String, robot: String): Unit = {
    if (result > 0) {
      val humanDetails = statistics.getOrElse(s"$role", null)
      if (humanDetails == null) statistics(s"$role") = new Details(2, 1, 0, 0)
      else {
        statistics(s"$role").score += 2
        statistics(s"$role").win += 1
      }

      val robotDetails = statistics.getOrElse(s"$robot", null)
      if (robotDetails == null) statistics(s"$robot") = new Details(0, 0, 0, 1)
      else {
//        statistics(s"$robot").win -= 1
        statistics(s"$robot").lose += 1
      }
    } else if (result == 0) {
      val humanDetails = statistics.getOrElse(s"$role", null)
      if (humanDetails == null) statistics(s"$role") = new Details(1, 0, 1, 0)
      else {
        statistics(s"$role").score += 1
        statistics(s"$role").draw += 1
      }

      val robotDetails = statistics.getOrElse(s"$robot", null)
      if (robotDetails == null) statistics(s"$robot") = new Details(1, 0, 1, 0)
      else {
        statistics(s"$robot").score += 1
        statistics(s"$robot").draw += 1
      }
    } else {
      val humanDetails = statistics.getOrElse(s"$role", null)
      if (humanDetails == null) statistics(s"$role") = new Details(0, 0, 0, 1)
      else {
//        statistics(s"$role").score -= 2
        statistics(s"$role").lose += 1
      }

      val robotDetails = statistics.getOrElse(s"$robot", null)
      if (robotDetails == null) statistics(s"$robot") = new Details(2, 1, 0, 0)
      else {
        statistics(s"$robot").score += 2
        statistics(s"$robot").win += 1
      }
    }
  }
}